package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.InFo.MoreSpecialSecondInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * 精彩专栏 第一个二级页面 适配器
 * Created by lanouhn on 16/9/18.
 */
public class MoreSpecialSecondAdapter extends BaseAdapter {
    private Context mContext;
    private MoreSpecialSecondInfo info;


    public MoreSpecialSecondAdapter(Context mContext, MoreSpecialSecondInfo info) {
        this.mContext = mContext;
        this.info = info;
    }

    @Override
    public int getCount() {
        return info.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return info.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_special_second, null);
            holder.pic = (ImageView) convertView.findViewById(R.id.pic_second);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.name = (TextView) convertView.findViewById(R.id.more_novel_details_title);
            holder.update = (TextView) convertView.findViewById(R.id.more_novel_details_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
/**
 * 解析网上获取的数据
 */
        Picasso.with(mContext).load(info.getData().get(position).getPic_url()).into(holder.pic);
        holder.name.setText(info.getData().get(position).getTitle());
        Long data = info.getData().get(position).getPublished() * 1000L;
        Date date = new Date(data);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm");
        String time = format.format(date);
        holder.update.setText(time);

/**
 * 判断该布局是否显示“视频”图片
 */
        if (info.getData().get(position).getVideo_url() != "") {
            holder.iv.setVisibility(View.VISIBLE);
        } else {
            holder.iv.setVisibility(View.GONE);
        }

/**
 * 判断该布局是否有图片
 */
        if (info.getData().get(position).getPic_url() == null) {
            holder.pic.setVisibility(View.GONE);
        } else {
            holder.pic.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView pic, iv;
        private TextView name, update;
    }
}
